package com.silence.springboot.convert;

import com.mybatisflex.core.paginate.Page;
import com.silence.springboot.entity.UserBaseInfoEntity;
import com.silence.springboot.enums.SexEnum;
import com.silence.springboot.vo.input.SearchByConditionInput;
import com.silence.springboot.vo.input.UserBaseInfoInput;
import com.silence.springboot.vo.output.QueryUserBaseInfoByTokenOutput;
import com.silence.springboot.vo.output.UserBaseInfoOutput;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

/**
 * @author silence
 * @email 792245022@qq.com
 */
@Mapper(componentModel = "spring")
public interface UserBaseInfoConvert {

    /**
     * 将用户基础信息表实体类转换为根据token查询用户信息的输出视图
     *
     * @param entity 用户基础信息表实体类
     * @return 根据token查询用户信息的输出视图
     */
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "enterTime", target = "enterTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "sex", target = "sexText", qualifiedByName = "sexEnumToString")
    QueryUserBaseInfoByTokenOutput toQueryUserBaseInfoByTokenOutput(UserBaseInfoEntity entity);

    /**
     * 修改用户基础信息入参转换
     *
     * @param input 修改用户基础信息入参
     * @return 用户基础信息表实体类
     */
    @Mapping(source = "id", target = "id")
    @Mapping(source = "token", target = "token", ignore = true)
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "enterTime", target = "enterTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    UserBaseInfoEntity updateToUserBaseInfoEntity(UserBaseInfoInput input);

    /**
     * 查询条件转换用户信息实体
     *
     * @param input 入参
     * @return 返回值
     */
    UserBaseInfoEntity searchByConditionInputToUserBaseInfoEntity(SearchByConditionInput input);

    /**
     * 条件查询用户基础信息出参转换
     *
     * @param entity 用户基础信息表实体类
     * @return 条件查询用户基础信息出参
     */
    @Mapping(source = "sex", target = "sexText", qualifiedByName = "sexEnumToString")
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "enterTime", target = "enterTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    UserBaseInfoOutput toUserBaseInfoOutput(UserBaseInfoEntity entity);

    /**
     * 分页查询用户基础信息出参转换
     *
     * @param page 分页查询用户基础信息出参
     * @return 分页查询用户基础信息出参
     */
    Page<UserBaseInfoOutput> toPageUserBaseInfoOutput(Page<UserBaseInfoEntity> page);

    /**
     * 新增用户基础信息入参转换
     *
     * @param input 新增用户基础信息入参
     * @return 用户基础信息表实体类
     */
    @Mapping(source = "id", target = "id", ignore = true)
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "enterTime", target = "enterTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    UserBaseInfoEntity addUserBaseInfoInputToUserBaseInfoEntity(UserBaseInfoInput input);

    /**
     * 性别枚举转换为字符串
     *
     * @param sex 性别枚举
     * @return 性别字符串
     */
    @Named(value = "sexEnumToString")
    default String sexEnumToString(String sex) {
        try {
            int sexCode = Integer.parseInt(sex);
            return SexEnum.getDescByCode(sexCode);
        } catch (Exception e) {
            return "";
        }
    }
}
